using Microsoft.EntityFrameworkCore.Storage.ValueConversion;

namespace ProxyServer.EntityFrameworkCore.ValueConverts;

public class SqliteDateTimeOffsetValueConverter : ValueConverter<DateTimeOffset, string>
{
    public SqliteDateTimeOffsetValueConverter() : base(
        v => v.UtcDateTime.ToString("yyyyMMdd HHmmss"),
        v => DateTimeOffset.Parse("yyyyMMdd HHmmss").ToLocalTime())
    {
    }

    public static SqliteDateTimeOffsetValueConverter Singleton { get; } = new();
}

public class SqliteTicksValueConverter : ValueConverter<DateTimeOffset, long>
{
    public SqliteTicksValueConverter() : base(
        v => ConvertToTicks(v),
        v => ConvertToLocalTime(v)
    )
    {

    }

    public static long ConvertToTicks(DateTimeOffset value)
    {
        // utc
        return value.UtcTicks;
    }

    public static DateTimeOffset ConvertToLocalTime(long tickes)
    {
        var time = new DateTimeOffset(tickes, TimeSpan.Zero);
        return time.ToLocalTime();
    }

    public static readonly SqliteTicksValueConverter Instance = new();
 }